local skynet = require "skynet"

local s = require "service"
local game_cmd = require "cmd_def"
local table_utils = require "table_utils"
local agent_def = require "agent_def"

local function get_player_base_info(gate, msg)
    local uid = msg.uid
    -- print("hotfix success")
    -- skynet.error("hotfix success")
    if s.player:get_uid() ~= uid then
        skynet.error("此接口不允许查看其他人的信息")
        return
    end

    local player = s.player

    local player_base_info = player:get_player_base_info()
    skynet.error("player base info:" .. table_utils.table2string(player_base_info))

    local response = {
        error_code = game_cmd.ERROR_CODE.SUCCESS,
        player_base_info = player_base_info
    }

    skynet.send(gate, "lua", "send", uid, game_cmd.SERVER_CMD.PLAYER_BASE_INFO, response)
end

local function request_enter_default_scene(gate, msg)
    local player = s.player
    if player:get_status() ~= agent_def.PLAYER_STATUS.LOGIN then
        skynet.error("玩家不在登录状态, 无法进入默认场景")
        return
    end

    local scene_id = player:get_scene_id()
    if scene_id == 0  then
        scene_id = 1001
    end
    skynet.error("player info:" .. table_utils.table2string(player))

    -- 向scene_mgr获取一个scene_node
    local isok, scene_node = skynet.call("scene_mgr", "lua", "get_scene_node", scene_id)
    if not isok then
        skynet.error("get scene_node failed")
        return
    end

    -- 请求进入scene
    local isok, new_scene_info = skynet.call(scene_node, "lua", "request_enter", gate, player)
    if not isok then
        skynet.error("enter scene failed")
        -- send error
        return
    end

    new_scene_info.scene_node = scene_node
    skynet.error("new_scene_info:" .. table_utils.table2string(new_scene_info))
    player:set_scene_info(new_scene_info)
    player:set_status(agent_def.PLAYER_STATUS.IN_SCENE)

    -- local response = {
    --     error_code = game_cmd.ERROR_CODE.SUCCESS,
    --     scene_info = player:get_scene_info()
    -- }
    -- skynet.send(gate, "lua", "send", player:get_uid(), game_cmd.SERVER_CMD.ENTER_DEFAULT_SCENE_RESPONSE, response)
end

local function request_move(gate, msg)
    skynet.error("agent request move, uid:" .. s.id)
    local player = s.player
    if player:get_status() ~= agent_def.PLAYER_STATUS.IN_SCENE then
        skynet.error("玩家不在场景, 无法移动")
        return
    end

    local scene_node = player:get_scene_node()
    if not scene_node then
        skynet.error("没有找到scene_node, 无法移动")
        return
    end

    local isok = skynet.call(scene_node, "lua", "request_move", s.id, msg)
end

local function request_use_skill(gate, msg)
    skynet.error("agent request use skill, uid:" .. s.id .. " skill id:" .. msg.skill_id)
    local player = s.player
    if player:get_status() ~= agent_def.PLAYER_STATUS.IN_SCENE then
        skynet.error("玩家不在场景, 无法使用技能")
        return
    end

    local scene_node = player:get_scene_node()
    if not scene_node then
        skynet.error("没有找到scene_node, 无法使用技能")
        return
    end

    local isok = skynet.call(scene_node, "lua", "request_use_skill", s.id, msg)
end

s.client_handle = {
    [game_cmd.CLIENT_CMD.GET_PLAYER_BASE_INFO] = get_player_base_info,
    [game_cmd.CLIENT_CMD.ENTER_DEFAULT_SCENE] = request_enter_default_scene,
    [game_cmd.CLIENT_CMD.MOVE] = request_move,
    [game_cmd.CLIENT_CMD.USE_SKILL] = request_use_skill,
}

s.resp.client = function(source, cmd, ...)
    if not s.player or s.player:get_status() == agent_def.PLAYER_STATUS.ERROR then
        return
    end

    local func = s.client_handle[cmd]
    if func then
        func(source, ...)
    else
        skynet.error("no func to handle agent client cmd:" .. cmd)
    end
end

